package com.lazydong.gui_gateway.route;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

public class RoutePropertyEnvironment implements EnvironmentPostProcessor, Ordered {
    public static final AtomicInteger port = new AtomicInteger(8888);
    public static String routePropertySources = "routePropertySources";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        int port = RoutePropertyEnvironment.port.get();
        if (port == 0) {
            throw new RuntimeException("port 没有配置");
        }
        Properties properties = new Properties();
        properties.put("server.port", port);
        if (!environment.getPropertySources().contains(routePropertySources)) {
            environment.getPropertySources().addLast(new PropertiesPropertySource(routePropertySources, properties));
        }
    }


    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

}